import 'package:flutter/material.dart';
import 'package:xws/models/user/feedback_obj.dart';
import 'package:xws/res/urls.dart';
import 'package:xws/state/user_state.dart';
import 'package:xws/ui/splash/splash_page.dart';
import 'package:xws/utils/app_util.dart';
import 'package:xws/utils/dio_utils/request/user_req.dart';
import 'package:xws/utils/storage_util.dart';
import 'package:xws/utils/theme_util.dart';
import 'package:xws/utils/tools_util.dart';
import 'package:xws/widgets/app_btn.dart';
import 'package:xws/widgets/safe_scaffold.dart';

import 'black_list.dart';
import 'clear_item.dart';
import 'feed_back.dart';

enum _SettingFuncs {
  feedback,
  aboutus,
  clearCache,
  blackList,
}

const List<String> _funcText = <String>[
  '意见反馈',
  '了解我们',
  '清理缓存',
  '黑名单',
];

extension _ExAccFunc on _SettingFuncs {
  String get name => _funcText[index];
}

/// 设置
class Settings extends StatelessWidget {
  const Settings({Key? key}) : super(key: key);

  /// 点击
  Future<void> _onTap(BuildContext context, _SettingFuncs sf) async {
    switch (sf) {
      case _SettingFuncs.feedback:
        final FeedbackObj _feedbackObj = FeedbackObj();
        _feedbackObj.fromUser(us.info);
        push(context, FeedBack(feedbackObj: _feedbackObj));
        break;
      case _SettingFuncs.aboutus:
        openH5(Urls.about);
        break;
      case _SettingFuncs.blackList:
        push(context, const BlackList());
        break;
      default:
    }
  }

  /// 退出登录
  Future<void> _logOut(BuildContext context) async {
    final bool? r = await tips('确认退出登录?');

    if (r == null || !r) return;

    loading(context);
    await userLogout();
    close(context);

    setV('token', null);
    us.clear();
    push(context, const SplashPage());
  }

  @override
  Widget build(BuildContext context) {
    return SafeScaffold(
      titleText: '设置',
      body: Padding(
        padding: const EdgeInsets.all(20.0),
        child: Column(
          children: <Widget>[
            ClipRRect(
              borderRadius: BorderRadius.circular(10),
              child: Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  ..._SettingFuncs.values.map(
                    (_SettingFuncs sf) {
                      if (sf == _SettingFuncs.clearCache) return const ClearItem();

                      return AppBtn(
                        onTap: () => _onTap(context, sf),
                        child: Row(
                          children: <Widget>[
                            Text(
                              sf.name,
                              style: TextStyle(
                                fontSize: 15,
                                fontWeight: FontWeight.bold,
                                color: black(context).withOpacity(0.7),
                              ),
                            ),
                            const Spacer(),
                            const Icon(Icons.arrow_forward_ios_rounded, size: 16),
                          ],
                        ),
                        padding: const EdgeInsets.symmetric(horizontal: 20),
                        alignment: Alignment.center,
                        color: white(context),
                        height: 56,
                        radius: 0,
                      );
                    },
                  ).toList(),
                ],
              ),
            ),
            const Spacer(),
            Padding(
              padding: const EdgeInsets.symmetric(horizontal: 20),
              child: AppBtn(
                onTap: () => _logOut(context),
                child: Text(
                  '退出登录',
                  style: TextStyle(
                    fontSize: 15,
                    fontWeight: FontWeight.bold,
                    color: white(context),
                  ),
                ),
                padding: const EdgeInsets.symmetric(horizontal: 20),
                alignment: Alignment.center,
                color: red(context),
                height: 50,
                radius: 10,
              ),
            ),
            const SizedBox(height: 50),
          ],
        ),
      ),
    );
  }
}
